package com.example.taobao

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_buy.*
import kotlinx.android.synthetic.main.activity_gouwuche.tv_show_price
import kotlinx.android.synthetic.main.activity_mine.*
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import java.lang.Exception
import java.net.URLEncoder
import java.text.DecimalFormat
import kotlin.concurrent.thread

class buy : AppCompatActivity() {
    val format = DecimalFormat("0.##")//保留两位小数
    var total=0.0
    private val gooselist: ArrayList<gooseInfo> = ArrayList<gooseInfo>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_buy)

        init()
        //使用Recycler
        val mRecyclerView = findViewById<RecyclerView>(R.id.buy_goods)
        val layoutManager = LinearLayoutManager(this)
        mRecyclerView.layoutManager = layoutManager
        val adapter = gooseAdapter(this, gooselist)
        mRecyclerView.adapter = adapter

        runOnUiThread {
            buy_num.text="共${adapter.itemCount}件,"
            tv_show_price.text="合计￥${format.format(total)}"
        }
        submit_order.setOnClickListener {
            for(i in 0..gooselist.size-1) {
                insert(i)
            }
            val intent=Intent()
            intent.putExtra("result",1)
            setResult(RESULT_OK,intent)
            finish()
        }

    }


    fun insert(index:Int){
        thread {
            try {
                val client = OkHttpClient()
                val requestBody = FormBody.Builder()
                    .add("goodsename", URLEncoder.encode("${gooselist[index].goosename}","utf-8"))
                    .add("goodsprice", URLEncoder.encode("${gooselist[index].price}","utf-8"))
                    .add("username", URLEncoder.encode("Eurus","utf-8"))
                    .add("goodspic", URLEncoder.encode("${gooselist[index].goosepic}","utf-8"))
                    .add("goodsnum", URLEncoder.encode("${gooselist[index].num}","utf-8"))
                    .add("goodsdescribe", URLEncoder.encode("这是件不错的商品","utf-8"))
                    .build()
                val request = Request.Builder()
                    .url("http://192.168.43.140:8080/aaa_war_exploded/addorder.jsp")
                    .post(requestBody)
                    .build()
                client.newCall(request).execute()
            }
            catch (e: Exception)
            {
                e.printStackTrace()
            }
        }
    }

    fun init(){
        var bundle=this.intent.extras
        var idlist: ArrayList<Int> = ArrayList<Int>()
        var namelist: ArrayList<String> = ArrayList<String>()
        var numlist:ArrayList<Int> = ArrayList<Int>()
        var piclist:ArrayList<String> = ArrayList<String>()
        var price:DoubleArray= DoubleArray(10)
        if (bundle != null) {
            idlist=ArrayList<Int>(bundle.getIntegerArrayList("idlist"))
            namelist=ArrayList<String>(bundle.getStringArrayList("namelist"))
            numlist=ArrayList<Int>(bundle.getIntegerArrayList("numlist"))
            piclist=ArrayList<String>(bundle.getStringArrayList("piclist"))
//            price= bundle.getDoubleArray("price")
            total=bundle.getDouble("total_price")
        }
        if(bundle!=null){
            for (i in 0..idlist.size-1){
                price[i]= bundle.getDoubleArray("pricelist")?.get(i)!!
            }
        }
        var i=0
        for (i in 0..idlist.size-1){
            gooselist.add(gooseInfo(idlist[i],false,namelist[i],price[i],piclist[i],numlist[i]))
        }
    }
    inner class gooseAdapter(val content:Context,private val  gooselist: ArrayList<gooseInfo>) :
        RecyclerView.Adapter<gooseAdapter.MyViewHolder>() {

        private var ItemClickListenr:ItemClickListenr?=null
        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): MyViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.buy_goods, parent, false)
            return MyViewHolder(view)
        }

        fun setOnItemClickLinstenner(listenr: ItemClickListenr){
            this.ItemClickListenr=listenr
        }

        override fun getItemCount(): Int = gooselist.size ?: 0
        override fun onBindViewHolder(holder: gooseAdapter.MyViewHolder, position: Int) {
            val goose = gooselist[position]
            holder.goosename.text = goose.goosename
            Glide.with(content)
                .load(goose.goosepic)
                .into(holder.goosepic)
            holder.gooseprice.text =(format.format((goose.price)*(goose.num))).toString()
            holder.goosenum.text="x"+goose.num.toString()
            holder.itemView.setOnClickListener {

            }
        }

        inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val goosepic: ImageButton = itemView.findViewById(R.id.goose_picture)
            val goosename: TextView = itemView.findViewById(R.id.goose_name)
            val goosenum: TextView = itemView.findViewById(R.id.goose_num)
            val gooseprice: TextView = itemView.findViewById(R.id.goose_price)
        }
    }
}